package org.jboss.netty.channel.socket.oio;

import java.util.concurrent.Executor;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelSink;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.SocketChannel;
import org.jboss.netty.util.internal.ExecutorUtil;

/**
 * Me hubiera gustado poder heredar de OioClientSocketChannelFactory, pero no me
 * deja sobreescribir el sink
 * 
 * @author leo
 * 
 */
public class SerialChannelFactory implements ClientSocketChannelFactory {

	private Executor workerExecutor;
	private ChannelSink sink;

	public SerialChannelFactory(final Executor workerExecutor) {
		this.workerExecutor = workerExecutor;
		this.sink = this.createSink();
	}

	private OioClientSocketPipelineSink createSink() {
		return new SerialClientSocketPipelineSink(this.workerExecutor);
	}

	public SocketChannel newChannel(final ChannelPipeline pipeline) {
		return new SerialClientSocketChannel(this, pipeline, this.sink);
	}

	public void releaseExternalResources() {
		ExecutorUtil.terminate(this.workerExecutor);
	}

}